package action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import proxy.ProxyFactory;
import service.BranchService;
import service.BranchServiceInterface;
import utils.GetHelper;

import beans.Agent;
import beans.Branch;
import beans.BranchSMSTemplate;
import beans.Terminal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.AgentDao;
import dao.BranchDao;
import dao.BranchSMSTemplateDao;
import dao.TerminalDao;

@SuppressWarnings("serial")
public class BranchAction extends ActionSupport implements ModelDriven<Branch>{

    private Branch branch = new Branch();
    private BranchSMSTemplate template = new BranchSMSTemplate();

    private int rows;// easyui页大小
    private int page;// easyui当前页
    private String ids;//删除id集合

    private int branchID;
    private int agentID;
    private String branchCity;
    private String branchProvince;
    private String smsTemplate;
    private List<Branch> branchs;
    private String returnType;// 返回类型
    private String pageName;//返回页面
    private final String urlPath = "http://112.124.59.126:8080/YouBoxApi/controller?";
    private Map<String, Object> branchMap;

    List<Agent> agents;
    List<BranchSMSTemplate> templates;
    List<String> agentSMS;

    private BranchDao branchDao = new BranchDao();
    private TerminalDao terminalDao = new TerminalDao();
    private AgentDao agentDao = new AgentDao();
    BranchSMSTemplateDao templateDao = new BranchSMSTemplateDao();

    BranchServiceInterface branchService = (BranchServiceInterface) ProxyFactory.getProxyInstance(new BranchService());

    ActionContext actionContext = ActionContext.getContext();
    Map<String, Object> session = actionContext.getSession();
    Agent agent = (Agent) session.get("admin");

    /**
     * 
     * 返回页面
     * @author Devil Zheng
     * @created 2015-9-15 下午3:59:37
     * @return
     */
    public String returnJSP() {
        return pageName;
    }
    
    /**
     * 获取网点分页
     * 
     * @return
     */
    public String getBranchPage() {
        if (returnType.equals("json")) {
            branchMap = new HashMap<String, Object>();
            branchMap.put("total", branchService.getBranchNumber(agent));
            branchMap.put("rows", branchService.getBranches(agent, rows, page));
            return "jsonSuccess";
        }else{
            return "branchList";
        }
    }
    
    /**
     * 新增网点
     * 
     * @return
     */
    public String addBranch() {
        branchMap = new HashMap<String, Object>();
        try {
            branch.setAgent(agent);
            branchDao.save(branch);
            template.setBranchId(branch.getBranchId() + "");
            template.setSmsTemplate(smsTemplate);
            templateDao.save(template);
            branchMap.put("success", true);
            branchMap.put("msg", "保存成功");
        } catch (Exception e) {
            branchMap.put("success", false);
            branchMap.put("msg", "保存失败："+e.toString());
        }
        return "jsonSuccess";
    }
    /**
     * 
     * 删除
     * @author Devil Zheng
     * @created 2015-9-12 下午9:51:21
     * @return
     */
    public String deleteBranch(){
        branchMap = new HashMap<String, Object>();
        try {
            branchService.deleteBranch(ids);
            branchMap.put("success", true);
            branchMap.put("msg", "删除成功");
        } catch (Exception e) {
            branchMap.put("success", false);
            branchMap.put("msg", "删除失败"+e.toString());
        }
        return "jsonSuccess";
    }
    
    /**
     * 
     * 修改
     * @author Devil Zheng
     * @created 2015-9-12 下午10:29:38
     * @return
     */
    public synchronized String editBranch() {
        branchMap = new HashMap<String, Object>();
        try {
            List<Terminal> terminals = terminalDao.returnTermialByBranchID(branchID);
            branch.setBranchId(branchID);
            branch.setAgent(agent);
            branch.setBranchCity(branchCity);
            branch.setBranchProvince(branchProvince);
            branchDao.edit(branch);
            templates = templateDao.getByBranchId(branchID);
            if (templates.size() == 1) {
                template = templates.get(0);
                template.setSmsTemplate(smsTemplate);
                templateDao.edit(template);
            } else {
                template.setBranchId(branch.getBranchId() + "");
                template.setSmsTemplate(smsTemplate);
                templateDao.save(template);
            }
            for (Terminal t : terminals) {
                GetHelper.doHttpGet(urlPath + "funcID=0029&terminalId=" + t.getTerminalId());
            }
            branchMap.put("success", true);
            branchMap.put("msg", "删除成功");
        } catch (Exception e) {
            branchMap.put("success", false);
            branchMap.put("msg", "删除失败"+e.toString());
        }
        return "jsonSuccess";
    }

    public String getByBranchID() {
        branch = branchDao.getBranchById(branchID);
        branchs = new ArrayList<Branch>();
        branchs.add(branch);
        return "getBranch";
    }

    /**
     * 返回相应页面
     * 
     * @return
     */
    public String returnSmsTemplate() {
        agentSMS = new ArrayList<String>();
        String[] temp = agent.getSmsplatform().split(",");
        for (int i = 0; i < temp.length; i++) {
            agentSMS.add(temp[i]);
        }
        return "smsTemplate";
    }

    /**
     * 通过城市获取代理商信息
     * 
     * @return
     */
    public String getAgentByCity() {
        agents = agentDao.getAllAgent();
        return "jsonSuccess";
    }

    /**
     * 获取网点信息
     * 
     * @return
     */
    public String getBranchById() {
        branch = branchDao.getBranchById(branchID);
        branchProvince = branch.getBranchProvince();
        branchCity = branch.getBranchCity();
        templates = templateDao.getByBranchId(branchID);
        if (templates.size() == 1) {
            template = templates.get(0);
        }
        return "success";
    }

    /**
     * 获取网点与代理商信息
     * 
     * @return
     */
    public String getBranchAgentList() {
        branch = branchDao.getBranchById(branchID);
        // agent = agentDao.getAgentById(agentID);
        templates = templateDao.getByBranchId(branchID);
        if (templates.size() == 1) {
            template = templates.get(0);
        }
        return "success";
    }

    /**
     * 获取所有网点
     * 
     * @return
     */
    public String getAllBranchOfpack() {
        ActionContext actionContext = ActionContext.getContext();
        Map<String, Object> session = actionContext.getSession();
        Agent admin = (Agent) session.get("admin");
        branchs = branchDao.getAll(admin);
        return "selectBranch";
    }

    public List<Branch> getBranchs() {
        return branchs;
    }

    public void setBranchs(List<Branch> branchs) {
        this.branchs = branchs;
    }

    public String getReturnType() {
        return returnType;
    }

    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    public Map<String, Object> getBranchMap() {
        return branchMap;
    }

    public void setBranchMap(Map<String, Object> branchMap) {
        this.branchMap = branchMap;
    }

    public int getBranchID() {
        return branchID;
    }

    public void setBranchID(int branchID) {
        this.branchID = branchID;
    }

    public Branch getBranch() {
        return branch;
    }

    public void setBranch(Branch branch) {
        this.branch = branch;
    }

    public int getAgentID() {
        return agentID;
    }

    public void setAgentID(int agentID) {
        this.agentID = agentID;
    }

    public List<Agent> getAgents() {
        return agents;
    }

    public void setAgents(List<Agent> agents) {
        this.agents = agents;
    }

    public String getBranchCity() {
        return branchCity;
    }

    public void setBranchCity(String branchCity) {
        this.branchCity = branchCity;
    }

    public String getBranchProvince() {
        return branchProvince;
    }

    public void setBranchProvince(String branchProvince) {
        this.branchProvince = branchProvince;
    }

    public BranchSMSTemplate getTemplate() {
        return template;
    }

    public void setTemplate(BranchSMSTemplate template) {
        this.template = template;
    }

    public List<BranchSMSTemplate> getTemplates() {
        return templates;
    }

    public void setTemplates(List<BranchSMSTemplate> templates) {
        this.templates = templates;
    }

    public Agent getAgent() {
        return agent;
    }

    public void setAgent(Agent agent) {
        this.agent = agent;
    }

    public String getSmsTemplate() {
        return smsTemplate;
    }

    public void setSmsTemplate(String smsTemplate) {
        this.smsTemplate = smsTemplate;
    }

    public List<String> getAgentSMS() {
        return agentSMS;
    }

    public void setAgentSMS(List<String> agentSMS) {
        this.agentSMS = agentSMS;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    @Override
    public Branch getModel() {
        return branch;
    }

}
